#ifndef __zbControl_H
#define __zbControl_H

#include "main.h"

// states of data 
#define	idle			0x01
#define waitingfordata  0x02
#define gotnewdata		0x03
#define processingdata  0x04
#define processingcmd	0x05
#define creatingresp	0x06
#define readytosenddata	0x07
#define sendingdata		0x08
#define	dataerror		0x09

// periph enabled
#define enPWM1	0x01
#define enADC	0x02
#define enFQ1	0x04
#define enI2C	0x08
#define enGPIO	0x10

// errors, up to 16 stored in status, top 16 bits
#define ERR_SYSTICK 	0x80000000
#define ERR_PWM		 	0x40000000
#define ERR_BADCMD		0x20000000 
#define ERR_I2CTO		0x10000000
#define ERR_PKT_OF		0x8000000 // buffer overflow in circular buffer
#define ERR_RF			ERR_PKT_OF/2

// all globals used throughout the bot
#define g_VarsSize		100
extern u8 g_Vars[g_VarsSize]; // 100 bytes for our "global" vars
//extern u8 newg_Vars[g_VarsSize];

#pragma pack(push, 1)
// size 81 bytes
typedef struct glbVars
{
	// READ VARIABLES ********************************************
	 
	// enviromental
	u32 status;			// current status including errors
	u32 update;			// zbot requests you update something 
	
	// adc conversions
	u32 ADCIN1;			// 
	u32 ADCIN2;			// 
	u32 FQ1;			// captures the frequency on this pin

	// ReadWrite Variables, we will read often, write rarely

	// enviromental
	u32 gpioRead;	  	// live values of readable gpio
	u32 seconds;	  	// current seconds 
	u32 milliseconds;  	// current milliseconds
	u32 i2cRXBuff;		// last rx command from i2c

	// some space for a few future use vars
	u32 FutureR1;			//	future use ? 
	u32 FutureR2;			//	future use ? 
	
	// WRITE VARIABLES (CAN BE READ ALSO) ***********************

	u32 enabled;		// what perih are enabled
	u32 gpioWrite;		// write to gpio 
	// pwm motor control ? 
	u8 PWM1CH1Duty;	   	// 
	u8 PWM1CH2Duty;
	u8 PWM1CH3Duty;
	u8 PWM1CH4Duty; 
	u32 PWM1FQ;		   	// frequency of pwm
	// not used yet
	u32 i2cTXBuff;	   	// data sent to i2c bus
	u32 DAC1;			// dac1 
	u32 DAC2;			// dac2 
	// some space for a few future use vars
	u32 FutureW1;		//	future use ? 
	u32 FutureW2;		//	future use ? 

	// internal should not be read or written!
	u32 dataState;		// current state of data tx/rx in main

} globalVars, *pgloabalVars;
#pragma pack(pop)

extern pgloabalVars newg_Vars;


void doTasks(void);
void startupSystems(void);
//void doMotors(void);
//void doI2C(void);
//void gotosleep(void);


#endif
